package com.daffodil.workable.entity;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.GenericGenerator;

import com.daffodil.core.annotation.Hql;
import com.daffodil.core.annotation.Hql.Logical;
import com.daffodil.core.entity.BaseEntity;
import com.daffodil.util.JacksonUtils;
import com.daffodil.util.StringUtils;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * -组件执行结果
 * @author yweijian
 * @date 2022年4月6日
 * @version 1.0
 * @description
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "wok_result")
public class WorkableResult extends BaseEntity<String> {
    
    private static final long serialVersionUID = 2124686587594912771L;

    /** 主键编号 */
    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid")
    @Column(name = "id")
    private String id;
    
    /** 追踪ID */
    @Column(name = "trace_id")
    @Hql(type = Logical.LIKE)
    private String traceId;
    
    /** 任务ID */
    @Column(name = "task_id")
    @Hql(type = Logical.EQ)
    private String taskId;
    
    /** 任务名称 */
    @Transient
    private String taskName;
    
    /** 节点ID */
    @Column(name = "node_id")
    @Hql(type = Logical.EQ)
    private String nodeId;
    
    /** 节点名称 */
    @Transient
    private String nodeName;
    
    /** 节点执行状态 */
    @Column(name = "status")
    @Hql(type = Logical.EQ)
    private Boolean status;

    /** 消息内容 */
    @Column(name = "message", length = 8192)
    @Hql(type = Logical.LIKE)
    private String message;
    
    /** 创建时间 */
    @Column(name = "create_time")
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
    private Date createTime;
    
    /** 输入输出参数 */
    @Column(name = "variables", length = 8192)
    private String content;
    
    /** 运行模式 */
    @Column(name = "runmode")
    @Hql(type = Logical.EQ)
    private String runmode;

    /** 输入输出参数 */
    @Transient
    @JsonIgnore
    private Map<String, Object> variables = new HashMap<String, Object>();
    
    public void setVariables(Map<String, Object> variables){
        this.variables = variables;
        this.content = JacksonUtils.toJSONString(variables);
    }
    
    public WorkableResult success() {
        this.status = true;
        return this;
    }
    
    public WorkableResult error(String message, Object ...params) {
        this.status = false;
        this.message = StringUtils.format(message, params);
        return this;
    }

    public WorkableResult() {
        super();
    }
    
    public WorkableResult(String id, String traceId, String taskId, String taskName, String nodeId, String nodeName,
            Boolean status, String message, Date createTime) {
        super();
        this.id = id;
        this.traceId = traceId;
        this.taskId = taskId;
        this.taskName = taskName;
        this.nodeId = nodeId;
        this.nodeName = nodeName;
        this.status = status;
        this.message = message;
        this.createTime = createTime;
    }

    public WorkableResult(String id, String traceId, String taskId, String taskName, String nodeId, String nodeName,
            Boolean status, String message, Date createTime, String content) {
        super();
        this.id = id;
        this.traceId = traceId;
        this.taskId = taskId;
        this.taskName = taskName;
        this.nodeId = nodeId;
        this.nodeName = nodeName;
        this.status = status;
        this.message = message;
        this.createTime = createTime;
        this.content = content;
    }
    
}
